
<template>
  <div class="execution">
    <el-dialog
      :title="mode === 'import' ? '导入数据' : mode === 'verify' ? '确认数据' : '预览数据'"
      :visible="!!mode || !!previewTable"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      :destroy-on-close="true"
      width="80%"
    >
      <ImportData
        v-if="mode === 'import'"
        @uploadSuccess="mode = 'verify'"
        @close="handleClosedDialog"
      />
      <Verify
        v-else-if="mode === 'verify'"
        @done="handleClosedDialog"
        @close="handleClosedDialog"
      />
      <Preview
        v-else-if="previewTable !== null"
        @close="previewTable = null"
        :target="previewTable"
      />
    </el-dialog>
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
      >
        <template slot="menuRight">
          <el-button
            v-if="permissionList.import"
            type="primary"
            size="small"
            @click="handleImport"
          >导入收入日报表</el-button>
        </template>
        <template
          slot-scope="scope"
          slot="menu"
        >
          <el-button
            type="text"
            icon="el-icon-view"
            size="small"
            @click.stop="handleView(scope.row, scope.index)"
          >预览</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, exportTamplate, saveBatch, listPreview } from '@/api/rca/swckothenticketrevenuemanagementlog'
import { tableOption, startDate, endDate } from '@/const/crud/rca/swckothenticketrevenuemanagementlog'
import { mapGetters } from 'vuex'
import ImportData from "./components/ImportData";
import Verify from "./components/Verify";
import Preview from "./components/Preview";


export default {
  name: 'swckothenticketrevenuemanagement',
  components: {
    ImportData,
    Verify,
    Preview,
  },
  data () {
    return {
      searchForm: {
        updateTime: [startDate, endDate]
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,

      mode: null,
      previewTable: null,

      extraParam: {

      },
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        import: this.vaildData(this.permissions.sw_ck_othen_ticket_revenue_management_import, false),
      };
    }
  },
  methods: {
    async getList (page, params) {
      this.tableLoading = true;
      try {
        const { updateTime } = this.searchForm;
        const response = await fetchList(
          Object.assign(
            {
              startDate: updateTime && updateTime.length && updateTime[0] || null,
              endDate: updateTime && updateTime.length && updateTime[1] || null,
            },
          )
        );
        this.tableData = response.data.data.records;
        this.tableLoading = false;
      } catch (e) {
        this.tableLoading = false;
      };
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },

    handleView (row, index) {
      this.previewTable = row
    },
    handleImport () {
      this.mode = 'import'
    },
    searchReset () {
      this.page.currentPage = 1
      this.getList(this.page);
    },
    handleClosedDialog () {
      this.mode = null
      this.refreshChange()
    }
  }
}
</script>
